package com.graphics.ui;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.bin.activities.R;
import com.bin.comunication.ComunicationConnection;
import com.bin.intent1.PowerSlider;
import com.bin.intent1.PowerSliderBluetooth;
import com.bin.intent1.PowerSliderWriter;

public class LeftSideView extends View {
   	private float pozX,pozY;
	private float pozX1,pozY1;
	private float pozX2,pozY2;
	private float pozX3,pozY3;
	private boolean initPoz=false;
	private int deviation=40;
  
	
	private Paint backgroundPaint;
	private Paint backgroundSliderPaint;
	private Paint fillSliderPaint;
	
	private float cursorPosition=Float.MIN_VALUE;
	
	private PowerSlider ps=null;
	private ComunicationConnection bridge=null;
	
	public void setComunication(ComunicationConnection com){
		
		this.bridge=com;
	}
	
    public LeftSideView(Context context) {
		super(context);	initPoz=false;
		initLeftSideView();
	}
    
    public LeftSideView(Context context, AttributeSet attrs){
    	super(context,attrs); initPoz=false;
    	initLeftSideView();
    }

    public LeftSideView(Context context, AttributeSet attrs,int defaultStyle){
    	super(context,attrs,defaultStyle); 	initPoz=false;
    	initLeftSideView();
    }
   
    
    protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec){
    	//obtaining the shortest boundary height or width
    	
    	int measuredWidth =measure(widthMeasureSpec);
    	int measuredHeight=measure(heightMeasureSpec);
    		
    	setMeasuredDimension(measuredWidth,measuredHeight);
    }
    
    private int measure(int measureSpec) {
		
		int specMode=MeasureSpec.getMode(measureSpec);
		int specSize=MeasureSpec.getSize(measureSpec);
		
		if(specMode==MeasureSpec.UNSPECIFIED){
			return 200;
		}else{
			return specSize;
		}
	}
    
    protected void initLeftSideView(){
    	setFocusable(true);
    	
    	float ProcWidthP0=10,ProcHeightP0=10;
    	float ProcWidthP1=80,ProcHeightP1=10;
    	float ProcWidthP2=80,ProcHeightP2=30;
    	float ProcWidthP3=10,ProcHeightP3=30;
    	
    	
    	Resources r=this.getResources();
    	try{ProcWidthP0=Integer.parseInt(r.getString(R.string.CoordXP1));}catch(NumberFormatException e){ProcWidthP0=10;}
       	try{ProcWidthP1=Integer.parseInt(r.getString(R.string.CoordXP2));}catch(NumberFormatException e){ProcWidthP1=80;}
       	try{ProcWidthP2=Integer.parseInt(r.getString(R.string.CoordXP3));}catch(NumberFormatException e){ProcWidthP2=80;}
       	try{ProcWidthP3=Integer.parseInt(r.getString(R.string.CoordXP4));}catch(NumberFormatException e){ProcWidthP3=10;}
       	
       	try{ProcHeightP0=Integer.parseInt(r.getString(R.string.CoordYP1));}catch(NumberFormatException e){ProcHeightP0=10;}
       	try{ProcHeightP1=Integer.parseInt(r.getString(R.string.CoordYP2));}catch(NumberFormatException e){ProcHeightP1=10;}
       	try{ProcHeightP2=Integer.parseInt(r.getString(R.string.CoordYP3));}catch(NumberFormatException e){ProcHeightP2=30;}
       	try{ProcHeightP3=Integer.parseInt(r.getString(R.string.CoordYP4));}catch(NumberFormatException e){ProcHeightP3=30;}
    	
    	pozX=(float)ProcWidthP0/100.0f; pozY=(float)ProcHeightP0/100.0f; 
    		
    	pozX1=(float)ProcWidthP1/100.0f; pozY1=(float)ProcHeightP1/100.0f;
    	pozX2=(float)ProcWidthP2/100.0f; pozY2=(float)ProcHeightP2/100.0f;
    	pozX3=(float)ProcWidthP3/100.0f; pozY3=(float)ProcHeightP3/100.0f;
       
       	try{deviation=Integer.parseInt(r.getString(R.string.Deviation));}catch(NumberFormatException e){deviation=30;}
    
       	fillSliderPaint=new Paint(Paint.ANTI_ALIAS_FLAG); fillSliderPaint.setColor(r.getColor(R.color.fill_slider_color));
        backgroundPaint=new Paint(Paint.ANTI_ALIAS_FLAG); backgroundPaint.setColor(r.getColor(R.color.background_color));
        backgroundSliderPaint=new Paint(Paint.ANTI_ALIAS_FLAG); backgroundSliderPaint.setColor(r.getColor(R.color.background_slider_color));
        
    	this.setOnTouchListener(new OnTouchListener(){

			public boolean onTouch(View view, MotionEvent event) {
				setLineCoordinates(view,event);
				return false;
			}
    		
    	});
    	
    	this.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {	
			}
    		
    	});
    	
    } 
    
    private void setCursorPos(float px,float py){
	   if(px>=pozX&&px<=pozX1&&
 		       py>=pozY&&py<=pozY3){
 		    	cursorPosition=px;
 		    	ps.setStatus(cursorPosition);
 		    }
 		    else{
 		    	if(py>=pozY&&py<=pozY3){
 		    		if(px>pozX1) cursorPosition=pozX1;
 		    		else cursorPosition=Float.MIN_VALUE;
 		    	}
 		    	ps.setStatus(cursorPosition);
 		    }
	   
   	
    }
    
    private void setLineCoordinates(View view,MotionEvent event){
    	int action=event.getAction();
    	
    	float px,py;
    	switch(action){
    		case MotionEvent.ACTION_MOVE:
    			int historySize= event.getHistorySize();
    			for(int i=0;i<historySize;i++){
    				px=event.getHistoricalX(i);
    				py=event.getHistoricalY(i);
    					setCursorPos(px, py);
    				view.invalidate();
    			}
    			px=event.getX();
    			py=event.getY();
    				setCursorPos(px, py);
    			view.invalidate();
    		break;
    		
    		case MotionEvent.ACTION_DOWN:
    			px=event.getX();
    		    py=event.getY();
    				setCursorPos(px, py);
    		    view.invalidate();
    		break;
    		
    	} 
    }
    
    @Override
    protected void onDraw(Canvas canvas){

    	int height=getMeasuredHeight();
    	int width=getMeasuredWidth();
    	if(initPoz==false){
	    	pozX*=width; pozY*=height; 
	    	pozX1*=width; pozY1*=height;
	    	pozX2*=width; pozY2*=height;
	    	pozX3*=width; pozY3*=height;            
	    	ps=PowerSlider.getInstance(pozX, pozX1);
	    	@SuppressWarnings("unused")
			PowerSliderBluetooth psb = new PowerSliderBluetooth(ps,bridge);		
	    	@SuppressWarnings("unused")
			PowerSliderWriter psw=new PowerSliderWriter(ps);
	    	initPoz=true;
    	}
    	
    	Path path=new Path();
    
    	path.moveTo(pozX, pozY);
    	path.lineTo(pozX1, pozY1);
    	path.lineTo(pozX2, pozY2);
   		path.lineTo(pozX3, pozY3);
   		path.lineTo(pozX, pozY);
	
        path.addArc(new RectF(pozX1-deviation,pozY1,pozX1+deviation,pozY3), 90, -180);      	
    	path.addArc(new RectF(pozX-deviation,pozY1,pozX+deviation,pozY3),-90, -180);
        path.close();
        
    	canvas.drawPath(path, backgroundSliderPaint);
    	canvas.save();
    	
    	if(cursorPosition!=Float.MIN_VALUE){
    		Path fillPath=new Path();
    		fillPath.moveTo(pozX, pozY);
    		fillPath.lineTo(cursorPosition, pozY1);
    		fillPath.lineTo(cursorPosition, pozY2);
    		fillPath.lineTo(pozX3, pozY3);
    		fillPath.lineTo(pozX, pozY);
    	
    		fillPath.addArc(new RectF(pozX-deviation,pozY1,pozX+deviation,pozY3),-90, -180);
    		if(cursorPosition==pozX1){
    			fillPath.addArc(new RectF(pozX1-deviation,pozY1,pozX1+deviation,pozY3), 90, -180);      	
    		}
    		fillPath.close();
            
        	canvas.drawPath(fillPath, fillSliderPaint);
        		
    	}
    }

}